var PLEASE_SELECT = '---请选择---';

var LOADING_SELECT = '加载中...';

var DICTIONARY_FIND_URL = '/dictionary/findByVarGroup.do';
var AREA_FIND_URL = '/area/toSelect.do';

var Uec3PageSize = 15;

/**
 * 根据组查出字典对象集合
 * 
 * @param root_url
 * @param obj
 * @return
 */
function getDictionaryStore(root_url, obj) {
	return new Ext.data.Store({
				url : root_url + DICTIONARY_FIND_URL,
				// autoLoad : true,
				baseParams : obj,
				reader : new Ext.data.JsonReader({
							root : ''
						}, [{
									name : 'varIndex',
									type : 'int'
								}, {
									name : 'varName',
									type : 'string'
								}])
			});
}

/**
 * 根据字典数据集合得到下拉框对象
 * 
 * @param dictionaryStore
 * @param mode
 * @param name
 * @param fieldLabel
 * @param hiddenName
 * @return
 */
function getDictionaryComboBox(dictionaryStore, mode, name, fieldLabel,
		hiddenName, width, anchor) {
	return new Ext.form.ComboBox({
				typeAhead : true,
				fieldLabel : fieldLabel,

				name : name,
				anchor : anchor ? anchor : '',
				forceSelection : true,
				width : width ? width : 120,
				valueField : 'varIndex',
				displayField : 'varName',
				mode : mode,
				hiddenName : hiddenName,
				store : dictionaryStore,
				selectOnFocus : true,
				triggerAction : 'all',
				allowBlank : true,
				loadingText : LOADING_SELECT,
				editable : false,
				emptyText : PLEASE_SELECT
			});
}

/**
 * 记录
 * 
 * @return
 */
function getRecord(label, value) {
	var record = new Ext.data.Record.create([{
				name : value,
				type : 'int'
			}, {
				name : label,
				type : 'string'
			}]);
	return record;
}

/**
 * 根据组查出字典对象集合
 * 
 * @param url
 * @param obj
 * @param label
 * @param value
 * @return
 */
function getAreaStore(url, obj, label, value, record) {

	return new Ext.data.Store({
				url : url,
				autoLoad : false,
				pruneModifiedRecords : true,
				baseParams : obj,
				reader : new Ext.data.JsonReader({
							root : 'areas'
						}, record)
			});
}

/**
 * 地区记录
 * 
 * @param label
 * @param value
 * @return
 */
function getAreaRecord(label, value) {
	var area_record = getRecord(label, value);
	return area_record;
}

/**
 * 地区下拉框
 * 
 * @param fieldLabel
 * @param mode
 * @param hiddenName
 * @param store
 * @return
 */
function getAreaCombox(fieldLabel, displayField, mode, hiddenName, store,
		listeners) {

	var area_cmb = new Ext.form.ComboBox({

		fieldLabel : fieldLabel,
		// name : 'city',
		// anchor : '100%',

		width : 120,
		valueField : 'id',
		displayField : displayField,
		mode : mode,
		hiddenName : hiddenName,
		store : store,
		selectOnFocus : true,
		triggerAction : 'all',
		allowBlank : true,
		loadingText : LOADING_SELECT,
		editable : false,
		emptyText : PLEASE_SELECT,
		listeners : listeners
			// anchor:'100%'
		});
	return area_cmb;
}

/**
 * 省份下拉框
 * 
 * @param root_url
 * @return
 */
function getProvinceCombox(root_url, city_cmb, dist_cmb, mode, areaIdHidden) {
	var prov_record = getAreaRecord('province', 'id');
	var prov_store = getAreaStore(root_url + AREA_FIND_URL, {}, 'province',
			'id', prov_record);
	var listeners = {
		beforeselect : function() {
			this.hid_value = this.getValue();
		},
		select : function() {
			if (this.hid_value == this.getValue()) {
				return;
			}
			city_cmb.clearValue();
			city_cmb.getStore().removeAll();
			dist_cmb.clearValue();
			dist_cmb.getStore().removeAll();

			if (areaIdHidden) {
				areaIdHidden.setValue(this.getValue());
			}
			city_store.baseParams.prov = this.getValue();

			if (!city_cmb.MES_MAP[this.getValue()]) {

				city_store.load(city_cmb.city_load_callback);
			} else {
				city_store.removeAll();
				dist_cmb.getStore().removeAll();
				for (var i = 0; i < city_cmb.MES_MAP[this.getValue()].length; i++) {
					city_store.add(new city_record(city_cmb.MES_MAP[this
							.getValue()][i].data));
				}
			}
		}
	}

	// prov_store.load();
	var prov_cmb = null;
	if (mode) {
		prov_cmb = getAreaCombox('省份', 'province', mode, 'province',
				prov_store, listeners);
	} else {
		prov_cmb = getAreaCombox('省份', 'province', 'remote', 'province',
				prov_store, listeners);
	}
	var city_record = getAreaRecord('city', 'id');
	var city_store = city_cmb.getStore();

	city_cmb.MES_MAP = {};
	city_cmb.city_load_callback = {};
	city_cmb.city_load_callback.callback = function() {
		// alert(prov_cmb.getValue());
		if (!city_cmb.MES_MAP[prov_cmb.getValue()]) {
			city_cmb.MES_MAP[prov_cmb.getValue()] = city_store.data.items;
		}
	}

	return prov_cmb;
}

/**
 * 城市下拉框
 * 
 * @param root_url
 * @param dist_cmb
 * @return
 */
function getCityCombox(root_url, dist_cmb, areaIdHidden) {
	var city_record = getAreaRecord('city', 'id');
	var city_store = getAreaStore(root_url + AREA_FIND_URL, {}, 'city', 'id',
			city_record);
	var listeners = {
		beforeselect : function() {
			this.hid_value = this.getValue();
		},
		select : function() {

			if (this.hid_value == this.getValue()) {
				return;
			}
			dist_cmb.clearValue();
			dist_cmb.getStore().removeAll();
			if (areaIdHidden) {
				areaIdHidden.setValue(this.getValue());
			}
			dist_store.baseParams.city = this.getValue();

			if (!dist_cmb.MES_MAP[this.getValue()]) {

				dist_store.load(dist_cmb.dist_load_callback);
			} else {
				dist_store.removeAll();
				for (var i = 0; i < dist_cmb.MES_MAP[this.getValue()].length; i++) {
					dist_store.add(new dist_record(dist_cmb.MES_MAP[this
							.getValue()][i].data));
				}
			}
		}
	}
	var city_cmb = getAreaCombox('城市', 'city', 'local', 'city', city_store,
			listeners);

	var dist_record = getAreaRecord('district', 'id');
	var dist_store = dist_cmb.getStore();

	dist_cmb.MES_MAP = {};
	dist_cmb.dist_load_callback = {};
	dist_cmb.dist_load_callback.callback = function() {
		if (!dist_cmb.MES_MAP[city_cmb.getValue()]) {
			dist_cmb.MES_MAP[city_cmb.getValue()] = dist_store.data.items;
		}
	}

	return city_cmb;
}

/**
 * 区县下拉框
 * 
 * @param root_url
 * @return
 */
function getDistrictCombox(root_url, areaIdHidden) {
	var dist_record = getAreaRecord('district', 'id');
	var dist_store = getAreaStore(root_url + AREA_FIND_URL, {}, 'district',
			'id', dist_record);
	var listeners = {

		select : function() {

			if (this.hid_value == this.getValue()) {
				return;
			}

			if (areaIdHidden) {
				areaIdHidden.setValue(this.getValue());
			}

		}

	};

	var dist_cmb = getAreaCombox('区县', 'district', 'local', 'district',
			dist_store, listeners);
	return dist_cmb;
}

/**
 * 下拉框使用label作为有效数据时，转换‘请选择’为空
 * 
 * @param str
 * @return
 */
function castNoSelToNULL(str) {
	if (typeof(str) == 'string' && str.trim() == '') {
		return undefined;
	}
	if (str == PLEASE_SELECT) {
		return undefined;
	}
	return str;
}

/**
 * 初始化给三级下啦框赋值
 * 
 * @param provComb
 * @param cityComb
 * @param distComb
 * @param vo
 * @return
 */
function setAreaDefaultValue(provComb, cityComb, distComb, data) {
	// alert(data);
	var vo = Ext.decode(data.responseText);
	var provs = vo.provs;
	var cities = vo.cities;
	var dists = vo.dists;

	if (provs) {
		var prov_record = getAreaRecord('province', 'id');
		for (var i = 0; i < provs.length; i++) {
			provComb.getStore().add(new prov_record(provs[i]));
		}
		if (vo.area.provinceId) {
			provComb.setValue(vo.area.provinceId);
		} else {
			provComb.setValue(vo.area.id);
		}

	}
	if (cities) {
		var city_record = getAreaRecord('city', 'id');
		for (var i = 0; i < cities.length; i++) {
			cityComb.getStore().add(new city_record(cities[i]));
		}
		if (vo.area.cityId) {
			cityComb.setValue(vo.area.cityId);
		} else {
			cityComb.setValue(vo.area.id);
		}
	}
	if (dists) {
		var dist_record = getAreaRecord('district', 'id');
		for (var i = 0; i < dists.length; i++) {
			distComb.getStore().add(new dist_record(dists[i]));
		}
		distComb.setValue(vo.area.id);
	}

	// var pro_vo = {};
	// pro_vo.callback = function() {
	// this.setValue(vo.provId);
	//		
	// }.bind(proComb.getStore());
	// proComb.getStore().load(pro_vo);
	//	
	// cityComb.getStore().baseParams.prov = vo.provId;
	// var city_vo = {};
	// city_vo.callback = function() {
	// this.setValue(vo.cityId);
	// }.bind(cityComb.getStore());
	// cityComb.getStore().load(city_vo);
	//	
	// distComb.getStore().baseParams.city = vo.cityId;
	// var dist_vo = {};
	// dist_vo.callback = function() {
	// this.setValue(vo.distId);
	// }.bind(distComb.getStore());
	// distComb.getStore().load(dist_vo);

}

/**
 * 根据record得到里面的数据对象数组
 * 
 * @param {}
 *            records
 * @return {}
 */
function getRecordPropValue(recs) {
	var objs = [];
	Ext.each(recs, function(rec, index, recs) {
				objs[index] = rec.data;
			});
	return objs;
}

Ext.util.Format.comboRenderer = function(combo) {
	if (combo instanceof Ext.form.ComboBox) {
		return function(value) {
			// var record = combo.findRecord(combo.valueField, value);
			var rowIndex = combo.getStore().find(combo.valueField, value);
			// alert("render.rowIndex:"+rowIndex);
			if (rowIndex < 0)
				return '';
			var record = combo.getStore().getAt(rowIndex);
			// alert(Ext.encode(record.data));
			return record
					? record.get(combo.displayField)
					: combo.valueNotFoundText
		};
	}
};

/**
 * 采购销售订单通过字符串脚本获得本窗口时使用
 * 
 * @param {}
 *            id
 * @param {}
 *            creatorClazz
 * @return {}
 */
function getRunningWindow(id, creatorClazz) {
	var creator = Uec3.App.getModule(creatorClazz.prototype.id);
	return creator[id];

	// if(creatorClazz == Uec3.AddAutoPurchaseOrderWindowCreator) {
	// } else if(creatorClazz == AddSaleOrderWindowCreator) {
	// } else {}
}

/**
 * 单位
 * 
 * @param {}
 *            dictionaryStore
 * @param {}
 *            mode
 * @param {}
 *            name
 * @param {}
 *            fieldLabel
 * @param {}
 *            hiddenName
 * @param {}
 *            width
 * @param {}
 *            anchor
 * @return {}
 */
function getUnitComboBox(store, isBlank, mode, name, fieldLabel, hiddenName,
		width) {
	return new Ext.form.ComboBox({

				fieldLabel : fieldLabel,
				name : name,
				anchor : '100%',
				width : width ? width : 120,
				valueField : 'name',
				displayField : 'name',
				mode : mode,
				hiddenName : hiddenName,
				store : store,
				selectOnFocus : true,
				triggerAction : 'all',
				allowBlank : isBlank,
				loadingText : LOADING_SELECT,
				editable : false,
				emptyText : PLEASE_SELECT
			});
}
/**
 * 零件一级分类store
 * @param {} homeUrl
 * @return {}
 */
function getFirstCategoryStore(homeUrl) {

	var store_firstCategory = new Ext.data.JsonStore({
				url : homeUrl + "/part/category_getFirstCategorys.do",
				root : 'root',
				totalProperty : "totalProperty",
				fields : [{
							name : 'id',
							type : 'int'
						}, {
							name : 'level1Name',
							type : 'string'
						}]
			});
	return store_firstCategory;
}
/**
 * 得到零件一级分类
 * @param {} store
 * @param {} isBlank
 * @return {}
 */
function getFirstCategroyCombox(store, isBlank) {
	var combo_partner = new Ext.form.ComboBox({
				fieldLabel : '一级类别',
				store : store,
				displayField : 'level1Name',
				valueField : 'id',
				hiddenName : 'level1Id',
				typeAhead : false,
				mode : 'remote',
				triggerAction : 'all',
				emptyText : '请选择',
				minChars : 3,
				anchor : '100%',
				selectOnFocus : true,
				forceSelection : true,
				allowBlank : isBlank
			});
	return combo_partner;
}
/**
 * 得到零件二级分类
 * @param {} homeUrl
 * @return {}
 */
function getSecondCategoryStore(homeUrl) {

	var store_secondCategory = new Ext.data.Store({
				proxy : new Ext.data.HttpProxy({
							url :homeUrl+ '/part/category_getSubCategoryList.do'
						}),
				reader : new Ext.data.JsonReader({
							root : 'root',
							fields : [{
										name : 'id'
									}, {
										name : 'level2Name'
									}]
						})
			});
	return store_secondCategory;
}

function getSecondCategroyCombox(store,isBlank) {
var  combo_partner = new Ext.form.ComboBox({
        fieldLabel : '二级类别',
        store: store,
        displayField:'level2Name',
		valueField:'id',
		hiddenName:'partCategoryId',
        typeAhead: false,
        mode: 'local',
        triggerAction: 'all',
        emptyText:'请选择',
        minChars:3,
		anchor : '100%',
        selectOnFocus:true,
		forceSelection  :true,
		allowBlank : isBlank
   });
   return combo_partner;

}

function openSaleOrderById(id) {
	Ext.Ajax.request({
		url : RES_PATH + '/saleOrder/onlineToShow.do',
		params : {saleOrderId : id},
		success : function(res) {
			var saleOrderId = Ext.decode(res.responseText).saleOrderId;
			if(saleOrderId) {
				var desktop = Uec3.App.getDesktop();
				var codeOfSeller = Ext.decode(res.responseText).codeOfSeller;
				desktop.launchWindowByParam('addAutoSaleOrderWindowCreator', {init_sellerCode : codeOfSeller, id : id});
			}
		}
	});
}